//
//  TMAllMovieListView.m
//  movieInfo
//
//  Created by zhangkai on 2023/6/4.
//

#import "TMAllMovieListView.h"
#import "TMHomeSubVCCell.h"
#import "HomeModel.h"
@interface TMAllMovieListView()
@property(strong,nonatomic)NSArray *listArr;
@property(strong,nonatomic)UILabel *la;

@end
@implementation TMAllMovieListView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        
        self.backgroundColor = [UIColor blackColor];
        
        UILabel *la = [UILabel new];
        la.textColor = [UIColor whiteColor];
        la.font = [UIFont systemFontOfSize:14];
        [self addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
         
            make.top.equalTo(self.mas_top).offset(10);
            make.left.equalTo(self.mas_left).offset(15);
        }];
        self.la = la;
        
        
        
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
        [self addSubview:self.mainCollectionView];
        self.mainCollectionView.delegate = self;
        self.mainCollectionView.dataSource = self;
        [self.mainCollectionView registerClass:[TMHomeSubVCCell class] forCellWithReuseIdentifier:@"TMHomeSubVCCell"];
        
        [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.top.equalTo(self.la.mas_bottom).offset(10);
//            make.edges.equalTo(self);
        }];
        self.mainCollectionView.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];
        
        self.listArr = @[].copy;
        
    }
    
    return self;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.listArr.count;
    

}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TMHomeSubVCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMHomeSubVCCell" forIndexPath:indexPath];
    [cell configUI:self.listArr[indexPath.row]];
//    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;{
    return CGSizeMake((kScreenW-65)/2, 250);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;{
    return  UIEdgeInsetsMake(10, 25, 10, 25);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}

- (void)configWithID:(NSString *)idstr;{
    
    
    NSDictionary *dic = @{
        @"id":idstr,
        @"reg_id":@" ",
    };
    
    
    
    
    [[TMNetworkManage defaultManager] MVWithApiName:@"177/" parameters:dic success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        
        HomeModel *model = [HomeModel mj_objectWithKeyValues:responseObject];
        if(model.state == 200){
            
            self.la.text = [model.allDic objectForKey:@"name"];
            NSArray *arr = [model.allDic objectForKey:@"249"];
            self.listArr = arr.copy;
            [self.mainCollectionView reloadData];
            
            
        }

        
        
                
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        
    }];
    
    

    
    
}
@end
